#include <iostream>

using std::cout;
using std::endl;

class A
{
public:
    A()
    : _lx(0)
    {
        cout << "A()" << endl;
    }

    void setX(long lx)
    {
        _lx = lx;
    }

    void print() const
    {
        cout << "A::lx = " << _lx << endl;
    }

    ~A()
    {
        cout << "~A()" << endl;
    }
private:
    long _lx;
};

class B
: virtual public A
{
    //long _lx;
};

class C
: virtual public A
{
    //long _lx;
};

class D
: public B
, public C
{
public:
    D()
    {
        cout << "D()" << endl;
    }

    ~D()
    {
        cout << "~D()" << endl;
    }
    //long _lx;

};

void test()
{
    cout << "sizeof(A) = " << sizeof(A) << endl;//8
    cout << "sizeof(B) = " << sizeof(B) << endl;//8
    cout << "sizeof(C) = " << sizeof(C) << endl;//8
    cout << "sizeof(D) = " << sizeof(D) << endl;//8/16

    cout << endl;
    //2、多继承问题二：数据成员的存储二义性
    //解决方案：让B与C虚拟继承A（virtual）
    D d;
    d.B::setX(1);
    d.B::print();

    d.C::setX(100);
    d.C::print();

    d.A::setX(300);
    d.A::print();

    d.setX(500);
    d.print();
}

int main(int argc, char **argv)
{
    test();
    return 0;
}

